﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using KeRui.Common;

public partial class Manage_Cms_RoleItem : System.Web.UI.Page
{
    protected string ActName = PageRequest.GetString("actname");
    protected string act = PageRequest.GetString("act");
    protected int SiteID = PageRequest.GetInt("id", -1);
    protected string UserIDs = PageRequest.GetString("userids");
    protected string OtherIDs = PageRequest.GetString("otherids");

    protected KeRui.BLL.AdminRole bllAdminRole = new KeRui.BLL.AdminRole();
    protected KeRui.Model.AdminRole modelAdminRole = new KeRui.Model.AdminRole();
    protected List<KeRui.Model.AdminRole> listAdminRole;

    protected KeRui.BLL.Admin bllAdmin = new KeRui.BLL.Admin();
    protected List<KeRui.Model.Admin> listAdmin;
    protected KeRui.Model.Admin modelAdmin = new KeRui.Model.Admin();

    protected KeRui.BLL.Account bllAccount = new KeRui.BLL.Account();
    protected KeRui.Model.Account modelAccount = new KeRui.Model.Account();

    protected KeRui.BLL.AdminFlag bllAdminFlag = new KeRui.BLL.AdminFlag();
    protected List<KeRui.Model.AdminFlag> listAdminFlag;

    protected KeRui.BLL.CmsNode bllCmsNode = new KeRui.BLL.CmsNode();
    protected List<KeRui.Model.CmsNode> listCmsNode;
    protected void Page_Load(object sender, EventArgs e)
    {
        listAdminRole = bllAdminRole.GetList();

        switch (act)
        {
            case "edit": EditSave(); break;
        }
    }
    /// <summary>
    /// 修改用户名称
    /// </summary>
    protected void EditSave()
    {
        string[] userids = UserIDs.Split(',');

        string[] otherids = OtherIDs.Split(',');

        string NewUserFlag = "";

        for (int i = 0; i < userids.Length - 1; i++)
        {
            modelAdmin=bllAdmin.GetModel(Convert.ToInt32(userids[i].ToString()));

            if (modelAdmin.UserFlag.IndexOf("site_" + SiteID) == -1)
            {
                #region 添加默认权限
                listAdminFlag =bllAdminFlag.GetList("FlagType=1 and FlagGroup=1");

                listCmsNode = bllCmsNode.GetList(SiteID);

                foreach (KeRui.Model.AdminFlag modelAdminFlag in listAdminFlag)
                {
                    NewUserFlag += modelAdminFlag.Flag +"_"+SiteID+",";
                }
                foreach (KeRui.Model.CmsNode modelCmsNode in listCmsNode)
                {
                    NewUserFlag += SiteID + "_" + modelCmsNode.NodeID + "node,";
                }
                if (NewUserFlag == "")
                {
                    NewUserFlag = "site_" + SiteID;
                }
                else
                {
                    NewUserFlag = "site_" + SiteID + "," + NewUserFlag;
                }

                if (modelAdmin.PlusFlag == "")
                {
                    modelAdmin.PlusFlag = NewUserFlag;
                }
                else
                {
                    modelAdmin.PlusFlag = modelAdmin.PlusFlag + "," + NewUserFlag;
                }
                modelAdmin.UserFlag = "";
                if (modelAdmin.PlusFlag == "")
                {
                    if (modelAdmin.RoleFlags != "")
                    {
                        modelAdmin.UserFlag = modelAdmin.RoleFlags;
                    }
                }
                else
                {
                    if (modelAdmin.RoleFlags == "")
                    {
                        modelAdmin.UserFlag = modelAdmin.PlusFlag;
                    }
                    else
                    {
                        modelAdmin.UserFlag = modelAdmin.RoleFlags + "," + modelAdmin.PlusFlag;
                    }
                }
                bllAdmin.Update(modelAdmin,GetUserPwd(Convert.ToInt32(userids[i].ToString())));//添加操作
                #endregion
            }
        }
        for (int j = 0; j < otherids.Length - 1; j++)
        {
            modelAdmin = bllAdmin.GetModel(Convert.ToInt32(otherids[j].ToString()));

            if (modelAdmin.UserFlag.IndexOf("site_" + SiteID) != -1)
            {
                Regex regex = new Regex(@"site\w*?_" + SiteID);

                Regex reg = new Regex(SiteID + @"_\w*?node");

                MatchCollection collection = regex.Matches(modelAdmin.UserFlag);

                MatchCollection mc = reg.Matches(modelAdmin.UserFlag);

                foreach (Match match in collection)
                {
                    //把以前该网站的权限全部替换成空字符
                    modelAdmin.PlusFlag = modelAdmin.PlusFlag.Replace((match.Captures[0]).ToString(), "").Replace(",,", ",");
                }
                foreach (Match m in mc)
                {
                    modelAdmin.PlusFlag = modelAdmin.PlusFlag.Replace((m.Captures[0]).ToString(), "").Replace(",,", ",");
                }
                if (modelAdmin.PlusFlag == "")
                {
                    modelAdmin.PlusFlag = NewUserFlag;
                }
                else
                {
                    modelAdmin.PlusFlag = modelAdmin.PlusFlag;
                }
                modelAdmin.UserFlag = "";
                if (modelAdmin.PlusFlag == "")
                {
                    if (modelAdmin.RoleFlags != "")
                    {
                        modelAdmin.UserFlag = modelAdmin.RoleFlags;
                    }
                }
                else
                {
                    if (modelAdmin.RoleFlags == "")
                    {
                        modelAdmin.UserFlag = modelAdmin.PlusFlag;
                    }
                    else
                    {
                        modelAdmin.UserFlag = modelAdmin.RoleFlags + "," + modelAdmin.PlusFlag;
                    }
                }
                bllAdmin.Update(modelAdmin, GetUserPwd(Convert.ToInt32(otherids[j].ToString())));//添加操作
            }
        }

        Response.Write("<script type=\"text/javascript\">window.parent.location.href=\"sitelist.aspx?sucmsg=修改成功。\";</script>");
    }

    /// <summary>
    /// 获得用户密码
    /// </summary>
    /// <param name="UserID"></param>
    /// <returns></returns>
    protected string GetUserPwd(int UserID)
    {
        modelAccount=bllAccount.GetModel(UserID);
        return modelAccount.Password;
    }

    /// <summary>
    /// 获取用户名称
    /// </summary>
    /// <param name="RoleID"></param>
    /// <returns></returns>
    protected string GetUserName(string RoleID)
    {
        string content = "";

        if (RoleID == "")
        {
            listAdmin = bllAdmin.GetList("RoleIDs=''");
        }
        else
        {
            listAdmin = bllAdmin.GetList("RoleIDs like '%" + RoleID + "%'");
        }
        int i = 1;
        foreach (KeRui.Model.Admin modelAdmin in listAdmin)
        {
            if (modelAdmin.UserFlag.IndexOf("site_" + SiteID) != -1)
            {
                content += "<input type=\"checkbox\" checked=\"checked\" name=\"checked\" id=\""+modelAdmin.UserID+"\" />";
            }
            else
            {
                content += "<input type=\"checkbox\" name=\"checked\" id=\""+modelAdmin.UserID+"\" />";
            }
            content += modelAdmin.Username;
            if (i % 4 == 0)
            {
                content += "<br>";
            }
            i++;
        }
        return content;
    }

    //protected void EditSave()
    //{
    //    string[] roleids = RoleIDs.Split(',');
    //    string[] otherids = OtherIDs.Split(',');

    //    string NewSiteFlag = "";

    //    for (int i = 0; i < roleids.Length - 1; i++)
    //    {
    //        modelAdminRole = bllAdminRole.GetModel(int.Parse(roleids[i].ToString()));

    //        if (modelAdminRole.RoleFlag.IndexOf("site_" + SiteID) == -1)//原有数据中拥有该站点信息--则取出该站点的权限信息
    //        {
    //            NewSiteFlag = modelAdminRole.RoleFlag + ",site_" + SiteID;
    //            modelAdminRole.UpdateTime = DateTime.Now;
    //            modelAdminRole.RoleFlag = NewSiteFlag;
    //        }

    //        if (bllAdminRole.Update(modelAdminRole) == 1)
    //        {
    //            Response.Write("<script type=\"text/javascript\">alert('已有相同名称的角色。')</script>");
    //        }
    //        else
    //        {
    //            bllAdminRole.Update(modelAdminRole);
    //        }


    //    }
    //    if (otherids.Length - 1 != 0)
    //    {
    //        for (int j = 0; j < otherids.Length - 1; j++)
    //        {
    //            modelAdminRole = bllAdminRole.GetModel(int.Parse(otherids[j].ToString()));

    //            if (modelAdminRole.RoleFlag.IndexOf("site_" + SiteID) != -1)
    //            {
    //                NewSiteFlag = modelAdminRole.RoleFlag.Replace(",site_" + SiteID, "");
    //                modelAdminRole.UpdateTime = DateTime.Now;
    //                modelAdminRole.RoleFlag = NewSiteFlag;
    //            }
    //            if (bllAdminRole.Update(modelAdminRole) == 1)
    //            {
    //                Response.Write("<script type=\"text/javascript\">alert('已有相同名称的角色。')</script>");
    //            }
    //            else
    //            {
    //                bllAdminRole.Update(modelAdminRole);
    //                Response.Write("<script type=\"text/javascript\">window.parent.location.href=\"sitelist.aspx?sucmsg=修改成功。\";</script>");
    //            }
    //        }
    //    }
    //    else
    //    {
    //        Response.Write("<script type=\"text/javascript\">window.parent.location.href=\"sitelist.aspx?sucmsg=修改成功。\";</script>");
    //    }
    //}
}
